package com.ophiux.FeverManagement.aplication.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.FeverManagement.aplication.bean.addPatientTreatmentRequest;
import com.ophiux.FeverManagement.aplication.bean.addPatientTreatmentResponse;
import com.ophiux.FeverManagement.aplication.service.IAddPatientTreatmentAppPort;
import com.ophiux.FeverManagement.domain.entity.DoctorInfo;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatment;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentDetail;
import com.ophiux.FeverManagement.domain.entity.RbPatientinfo;
import com.ophiux.FeverManagement.domain.entity.RbTreatment;
import com.ophiux.FeverManagement.domain.repository.IDoctorRepositories;
import com.ophiux.FeverManagement.domain.repository.IPatientRepositories;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentRepository;
import com.ophiux.FeverManagement.domain.repository.ITreatmentRepository;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.parser.AssemblyFactory;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AddPatientTreatmentAppService implements IAddPatientTreatmentAppPort {
	
	@Autowired
	IPatientTreatmentRepository patientTreatmentRepository;
	@Autowired
	IDoctorRepositories doctorRepositories;
	@Autowired
	IPatientRepositories patientRepositories;
	@Autowired
	ITreatmentRepository treatmentRepository;

	@Override
	public Result<addPatientTreatmentResponse> execute(addPatientTreatmentRequest request) {
		addPatientTreatmentResponse resp = new addPatientTreatmentResponse();
		try {
			if (!request.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			DoctorInfo doctor = doctorRepositories.getDoctorByOtherId(request.getOTHER_ID());
			if(doctor == null) {
				return Result.fail(ResultEnum.NotFoundDoctorInfo);
			}
			
			RbPatientinfo patient = patientRepositories.selectOneByKey(request.getPATIENT_ID());
			if(patient == null) {
				return Result.fail(ResultEnum.NotFoundPatient);
			}
			
			RbTreatment treatment = treatmentRepository.selectOneByKey(request.getTREATMENT_ID());
			if(treatment == null) {
				return Result.fail(ResultEnum.NotFoundTreatment);
			}
			
			
			RbPatientTreatment model = new RbPatientTreatment(request.getPATIENT_ID(), doctor.getUSER_ID(), request.getTREATMENT_ID());
			model.setTREATMENT_ADVISE(request.getTREATMENT_ADVISE());
			
			model.setDOCTOR_NAME(doctor.getSTAFF_NAME());
			
			model.setPATIENT_NAME(patient.getPATIENT_NAME());
			model.setID_NO(patient.getID_NO());
			model.setTEL(patient.getTEL());
			model.setADDRESS(patient.getADDRESS());
			
			model.setTREATMENT_TITLE(treatment.getTITLE());
			
			
			List<RbPatientTreatmentDetail> model1 = AssemblyFactory.instance().parse(request.getMEDICINE(), RbPatientTreatmentDetail.class);
			
			String ID = patientTreatmentRepository.save(model, model1);
			
			
			resp.setPATIENT_TREATMENT_ID(ID);
			resp.setRESULT("1");
			
		} catch (Exception e) {
			log.info("AddPatientTreatmentAppService=》execute(),程序异常：", e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success(resp);
	}

}
